package com.powernode.strategy.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.powernode.constant.AuthConstants;
import com.powernode.domain.LoginUser;
import com.powernode.mapper.LoginUserMapper;
import com.powernode.model.LoginUserDetails;
import com.powernode.strategy.LoginStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * @description:
 * @author: mky
 * @date: 2024/7/5 8:59
 * @version: 1.0
 */
@Service(AuthConstants.SYSUSERLOGIN)
public class SysUserLoginStrategyImpl implements LoginStrategy {
    @Autowired
    private LoginUserMapper loginUserMapper;
    @Override
    public UserDetails realLogin(String userName) {
        LoginUser loginUser = loginUserMapper.selectOne(new LambdaQueryWrapper<LoginUser>()
                .eq(LoginUser::getUsername, userName));
        if (ObjectUtil.isNull(loginUser)){
            throw new RuntimeException("用户不存在");
        }
        //查用户的权限
        Set<String> authSet = loginUserMapper.selectAuthByUserId(loginUser.getUserId());
        LoginUserDetails loginUserDetails = new LoginUserDetails(loginUser.getUserId(),
                loginUser.getUsername(), loginUser.getPassword(),
                loginUser.getStatus(), loginUser.getShopId(),AuthConstants.SYSUSERLOGIN,authSet);
        //封装数据并返回
        return loginUserDetails;
    }
}
